package cn.stronglink.assetmanage.business.inventory.adapter;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MainActivity;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.view.goods.GoodsDetailEditFragment;
import cn.stronglink.assetmanage.business.inventory.view.goods.GoodsDetailFragment;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.index.entity.PromissionEntity;
import cn.stronglink.assetmanage.utils.Constants;

import static cn.stronglink.assetmanage.R.drawable.inventory;


/**
 * Created by chen on 2017/6/16.
 */

public class InventoryOrderAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groupNameList;
    private Map<String,List<InventoryDetail>> itemNameList;

    public InventoryOrderAdapter(Context context, List<String> groupNameList) {
        this.context = context;
        this.groupNameList = groupNameList;
        this.itemNameList = new HashMap<>();
        for(int i=0;i<this.groupNameList.size();i++){
            this.itemNameList.put(this.groupNameList.get(i),new ArrayList<InventoryDetail>());
        }
    }

    public void setGroupNameList(List<String> groupNameList) {
        this.groupNameList = groupNameList;
    }

    public void setItemNameList(Map<String,List<InventoryDetail>> itemNameList) {
        this.itemNameList = itemNameList;
    }

    /**
     * 获取组的个数
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return groupNameList == null ? 0 : groupNameList.size();
    }

    /**
     * 获取指定组中的子元素个数
     *
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = this.groupNameList.get(groupPosition);
        return itemNameList == null ? 0 : itemNameList.get(key).size();
    }

    /**
     * 获取指定组中的数据
     *
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        String key = this.groupNameList.get(groupPosition);
        return groupNameList == null ? null : itemNameList.get(key);
    }

    /**
     * 获取指定组中的指定子元素数据
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = this.groupNameList.get(groupPosition);
        return itemNameList == null ? null : itemNameList.get(key).get(childPosition);
    }

    /**
     * 获取指定组的ID，这个组ID必须是唯一的
     *
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取指定组中的指定子元素ID
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 组和子元素是否持有稳定的ID，也就是底层数据的改变不会影响到他们
     *
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_inventory_order,null);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tv_inventory_order_name = (TextView)convertView.findViewById(R.id.tv_inventory_order_name);
            groupViewHolder.iv_inventory_open = (ImageView)convertView.findViewById(R.id.iv_inventory_open);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        if(isExpanded){
            groupViewHolder.iv_inventory_open.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.inventory_close));
        }else {
            groupViewHolder.iv_inventory_open.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.inventory_open));
        }
        groupViewHolder.tv_inventory_order_name.setText(groupNameList.get(groupPosition));
        return convertView;
    }

    /**
     * 获取一个视图对象，显示指定组中的指定子元素数据
     *
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemViewHolder itemViewHolder = null;
        final String groupName = groupNameList.get(groupPosition);
        final InventoryDetail goods = itemNameList.get(groupName).get(childPosition);  // 获取资产实例

        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_inventory_goods,null);
            itemViewHolder = new ItemViewHolder();
            itemViewHolder.tv_asset_name = (TextView)convertView.findViewById(R.id.tv_check_wait_asset_name);
            itemViewHolder.tv_asset_sn = (TextView)convertView.findViewById(R.id.tv_check_wait_sn);
            itemViewHolder.tv_asset_status = (TextView)convertView.findViewById(R.id.tv_check_wait_bind_status);

            TextView tvSn = convertView.findViewById(R.id.tv_check_wait_asset_sn);
            tvSn.setText("RFID");
            itemViewHolder.tv_asset_status.setVisibility(View.GONE);
            convertView.setTag(itemViewHolder);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MainActivity activity = (MainActivity) context;
                    PromissionEntity pe = new PromissionEntity();
                    pe.setTitle("资产详情");
                    if(groupPosition==0) {
                        pe.setFragment(new GoodsDetailEditFragment());
                    }else{
                        pe.setFragment(new GoodsDetailFragment());
                    }
                    pe.setPosition(MainActivity.FRAGMENT_CUSTOM_ID);

                    // 设置打开盘点详情后传递的参数
                    Bundle bundle=new Bundle();
                    bundle.putSerializable(Constants.RFID, goods.getRfidNo());
                    bundle.putSerializable(Constants.CHECK_IN_DETAIL_NO, goods.getOrderNo());
                    pe.setBundle(bundle);

                    activity.onEventRegister(pe);
                }
            });
        }else{
            itemViewHolder = (ItemViewHolder) convertView.getTag();
        }

        if(goods.getAssetName()==null || goods.getAssetName().isEmpty()){
            itemViewHolder.tv_asset_name.setText("未知");
        }else{
            itemViewHolder.tv_asset_name.setText(goods.getAssetName());

        }
        itemViewHolder.tv_asset_sn.setText(goods.getRfidNo());

        return convertView;
    }

    /**
     * 是否选中指定位置上的子元素
     *
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupViewHolder {
        TextView tv_inventory_order_name;
        ImageView iv_inventory_open;
    }

    class ItemViewHolder {
        TextView tv_asset_name;
        TextView tv_asset_sn;
        TextView tv_asset_status;
    }
}
